﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using FlickrNet;
using Bongiozzo.Properties;

namespace Bongiozzo.Models
{
    public class FlickrProject
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string PhotosetId { get; set; }
        public Photo PrimaryPhoto;
        public PhotosetPhotoCollection Photos;
    }
    
    public class FlickrContext
    {
        private Flickr flickr;
        public FlickrContext() 
        {
            flickr = new Flickr(Settings.Default.FlickrKey);
        }

        public List<FlickrProject> GetProjects(string language)
        {
            PhotosetCollection flickrPhotosets = flickr.PhotosetsGetList(Settings.Default.FlickrUserId);

            Regex reg_Project = new Regex(@"^Project\s+([^\|].*)\s+\|\s+(.*)");
            Match m;
            List<FlickrProject> returnProjects = new List<FlickrProject>();
            foreach (Photoset item in flickrPhotosets)
            {
                m = reg_Project.Match(item.Description);
                if (m.Success)
                {
                    FlickrProject prj = new FlickrProject();
                    if (language == "ru")
                        prj.Title = m.Groups[2].Value;
                    else
                        prj.Title = m.Groups[1].Value;
                    prj.Description = getDescription(item.Description, language);
                    prj.PhotosetId = item.PhotosetId;
                    prj.Photos = GetPhotosetPhotos(item.PhotosetId, language);
                    prj.PrimaryPhoto = prj.Photos.ToList().Find(
                        delegate(Photo ph) { return ph.PhotoId == item.PrimaryPhotoId; });
                    returnProjects.Add(prj);
                }
            }
            return returnProjects;
        }

        private string getTitle(string title, string language)
        {
            if (String.IsNullOrEmpty(title))
                return "";
            Regex reg_Title = new Regex(@"^([^\|].*)\s+\|\s+(.*)$");
            Match m;
            m = reg_Title.Match(title);
            if (m.Success)
                return m.Groups[language == "ru" ? 2 : 1].Value.Trim();
            return title;
        }

        private string getDescription(string desc, string language)
        {
            desc = Regex.Replace(desc, Settings.Default.FooterToReplace, @"", RegexOptions.IgnoreCase | RegexOptions.Multiline).Trim();
            if (String.IsNullOrEmpty(desc))
                return ""; 
            Regex reg_DescEn = new Regex(@"description english(.*)description english", RegexOptions.Singleline);
            Regex reg_DescRu = new Regex(@"description russian(.*)description russian", RegexOptions.Singleline);
            Match m;
            if (language == "ru")  {
                m = reg_DescRu.Match(desc);
            } else {
                m = reg_DescEn.Match(desc);
            }
            if (m.Success)
                return m.Groups[1].Value.Trim();
            return desc;
        }

        public PhotosetPhotoCollection GetPhotosetPhotos(string photosetId, string language)
        {
            PhotosetPhotoCollection photos = flickr.PhotosetsGetPhotos(photosetId,  PhotoSearchExtras.Description );
            foreach (Photo item in photos)
            {
                item.Title = getTitle(item.Title, language);
                //replace with Description later, when major release with correction will be published
                item.OriginalFormat = getDescription(item.Description, language);
            }
            return photos;
        }

        public PhotoCollection GetLastPhotos(int limit, string language, int page)
        {
            PhotoSearchOptions options = new PhotoSearchOptions();
            options.UserId = Settings.Default.FlickrUserId;
            options.SortOrder = PhotoSearchSortOrder.DatePostedDescending;
            options.MediaType = MediaType.Photos;
            options.Extras = PhotoSearchExtras.Description;
            options.PerPage = limit;
            options.Page = page;
            PhotoCollection photos = flickr.PhotosSearch(options);
            foreach (Photo item in photos)
            {
                item.Title = getTitle(item.Title, language);
                //replace with Description later, when major release with correction will be published
                item.OriginalFormat = getDescription(item.Description, language);
            }
            return photos;
        }
    }
}